<?php
/**
 * Class that operate on table 'things_article'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ThingsArticleMySqlDAO implements ThingsArticleDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ThingsArticleMySql 
	 */
	public function load($thingsArticle){
		$sql = 'SELECT * FROM things_article WHERE article_id = "'.$thingsArticle->article_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function articles_list($thingsArticle){
		$sql = 'SELECT * FROM things_article';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAll_article($thingsArticle){
		$sql = 'SELECT * FROM things_article order by '.$thingsArticle->order_by.' '.$thingsArticle->sort_by.' limit '.$thingsArticle->results_per_page.' offset '.$thingsArticle->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function article_count($thingsArticle){
		$sql = 'SELECT count(*) FROM things_article order by '.$thingsArticle->order_by.' '.$thingsArticle->sort_by.' limit '.$thingsArticle->results_per_page.' offset '.$thingsArticle->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param thingsArticle primary key
 	 */
	public function delete($thingsArticle){
		$sql = 'UPDATE things_article SET status = "'.$thingsArticle->status.'" WHERE article_id = "'.		$thingsArticle->article_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ThingsArticleMySql thingsArticle
 	 */
	public function insert($thingsArticle){
		$sql = 'INSERT INTO things_article (article_name, created_date, updated_date) VALUES ("'.$thingsArticle->article_name.'", "'.$thingsArticle->created_date.'", "'.$thingsArticle->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$thingsArticle->article_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ThingsArticleMySql thingsArticle
 	 */
	public function update($thingsArticle){
		$sql = 'UPDATE things_article SET article_name = ?, status = ?, updated_date = ? WHERE article_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($thingsArticle->article_name);
		$sqlQuery->setNumber($thingsArticle->status);
		$sqlQuery->set($thingsArticle->updated_date);
		$sqlQuery->setNumber($thingsArticle->article_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM things_article';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByArticleName($value){
		$sql = 'SELECT * FROM things_article WHERE article_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM things_article WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM things_article WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM things_article WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByArticleName($value){
		$sql = 'DELETE FROM things_article WHERE article_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM things_article WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM things_article WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM things_article WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ThingsArticleMySql 
	 */
	protected function readRow($row){
		$thingsArticle = new ThingsArticle();
		
		$thingsArticle->article_id = $row['article_id'];
		$thingsArticle->article_name = $row['article_name'];
		$thingsArticle->status = $row['status'];
		$thingsArticle->created_date = $row['created_date'];
		$thingsArticle->updated_date = $row['updated_date'];

		return $thingsArticle;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ThingsArticleMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>